%********************************************************************
%** Read out the TRMCO results for SImulation I                    **
%** output:absolute deviation of t statistics (Table 2)            **
%** Created by Min-Hua Huang, Dec 19, 2011                         **
%********************************************************************
clear; clc; format long; 
load trialdata1.mat; load TRMCO_result1.mat;
load getstats1.mat; a=0; b=1; adcoeff1=ones(10,2)*-999;
count1=0; count2=0;count3=0; count4=0;
sum1_1=0; sum2_1=0; sum3_1=0; sum4_1=0;
sum1_2=0; sum2_2=0; sum3_2=0; sum4_2=0;
sum5_1=0; sum6_1=0; sum7_1=0; sum8_1=0;
sum5_2=0; sum6_2=0; sum7_2=0; sum8_2=0;
for i=1:1:1000
    total=trialdata1(1:n(i),(i-1)*4+1:i*4);
    y=total(:,1);
    xs1=total(:,3)-mean(total(:,3));
    xs2=total(:,4)-mean(total(:,4));
    x=[ones(n(i),1) xs1 xs2];     
% absolute deviation of estimated t statistics
    Ltrm=Lpar(TRMCO_result1(i,7:10)',a,b,y,x)\eye(4);
    Ltrmco=Lpar(TRMCO_result1(i,13:16)',a,b,y,x)\eye(4);
    trmse=sqrt(diag(Ltrm));
    trmcose=sqrt(diag(Ltrmco));
    trmt=TRMCO_result1(i,7:10)'./trmse;
    trmcot=TRMCO_result1(i,13:16)'./trmcose;
    difft=abs(trmt-trmcot);    
% absolute deviation of estimate beta    
    if table6_1(i,9)==0        
       sum1_1=sum1_1+abs(TRMCO_result1(i,7)-TRMCO_result1(i,13));
       sum2_1=sum2_1+abs(TRMCO_result1(i,8)-TRMCO_result1(i,14));
       sum3_1=sum3_1+abs(TRMCO_result1(i,9)-TRMCO_result1(i,15));
       sum5_1=sum5_1+difft(1);
       sum6_1=sum6_1+difft(2);
       sum7_1=sum7_1+difft(3);       
       count1=count1+1;
    else
       sum1_2=sum1_2+abs(TRMCO_result1(i,7)-TRMCO_result1(i,13)); 
       sum2_2=sum2_2+abs(TRMCO_result1(i,8)-TRMCO_result1(i,14));
       sum3_2=sum3_2+abs(TRMCO_result1(i,9)-TRMCO_result1(i,15));
       sum5_2=sum5_2+difft(1);
       sum6_2=sum6_2+difft(2);
       sum7_2=sum7_2+difft(3);        
       count2=count2+1;
    end
    if TRM_result1(i,5)>0
       if table6_1(i,9)==0
          sum4_1=sum4_1+abs(TRMCO_result1(i,10)-TRMCO_result1(i,16));
          sum8_1=sum8_1+difft(4); 
          count3=count3+1;
       else
         sum4_2=sum4_2+abs(TRMCO_result1(i,10)-TRMCO_result1(i,16));    
         sum8_2=sum8_2+difft(4); 
        count4=count4+1;
       end 
    end
    display(i);
end    
adcoeff1(1,1)=sum1_1/count1; adcoeff1(1,2)=sum1_2/count2;
adcoeff1(2,1)=sum2_1/count1; adcoeff1(2,2)=sum2_2/count2;
adcoeff1(3,1)=sum3_1/count1; adcoeff1(3,2)=sum3_2/count2;
adcoeff1(4,1)=sum4_1/count3; adcoeff1(4,2)=sum4_2/count4;
adcoeff1(5,1)=sum5_1/count1; adcoeff1(5,2)=sum5_2/count2;
adcoeff1(6,1)=sum6_1/count1; adcoeff1(6,2)=sum6_2/count2;
adcoeff1(7,1)=sum7_1/count1; adcoeff1(7,2)=sum7_2/count2;
adcoeff1(8,1)=sum8_1/count3; adcoeff1(8,2)=sum8_2/count4;
adcoeff1(9,1)=count1; adcoeff1(9,2)=count2;
adcoeff1(10,1)=count3; adcoeff1(10,2)=count4;
save adt1.mat